package com.sgb.kjwl.viewmodel.self;

import android.content.Intent;
import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.authentication.OrganizationPostSuccess;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.event.self.AuthenticationEvent;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.self.authentication.CertificationActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CertificationModel {
    //    public ObservableField<SupplierAuthEntity> authEntity = new ObservableField<>();
    private CertificationActivity activity;
    private SelfRepertory repertory;

    public CertificationModel(CertificationActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void commitAuth(String name, String cardNo,String type) {
        DialogHelper.showProgressDialog(activity, null, "提交中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().identificationPerson(name, cardNo).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }

                if (response.body().getState().equals("ok")) {
                    if(response.body().getData().equals("1")){
                        MToast.showToast(activity, "认证成功");
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERSON_AUTH, true);
                        AuthenticationEvent authenticationEvent = new AuthenticationEvent();
                        authenticationEvent.setMessageTag(EventConstant.EVENT_USER_AUTHENTICATION);
                        EventBus.getDefault().postSticky(authenticationEvent);
                        EventBus.getDefault().postSticky(LoginEvent.make(EventConstant.EVENT_MAIN_INDEX, 0));  //主动通知刷新消息通讯录数据
                        if (TextUtils.isEmpty(type)){
                            MainActivity.start(activity);
                        } else {
                            activity.finishAndData();
                        }
                        //给SharedPreferenceUtil中的REAL_NAME赋值 保存实名认证的状态
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_NAME, name);
                        //保存个人认证状态
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ID_CARD, cardNo);
                    }else {
                        Intent intent =new Intent(activity, OrganizationPostSuccess.class);
                        intent.putExtra("from","1");
                        activity.startActivity(intent);
                        activity.finish();
                    }

                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "认证失败, 请重试");
            }
        });

    }
}
